package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.baselibrary.base.NoDataViewHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.HouseManagerBean;
import com.cmccgz.udacs_landlord_webapp.main.cardmanager.CardAccreditActivity;
import com.cmccgz.udacs_landlord_webapp.main.housemanager.list.HouseListActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/8 15:03
 */

public class HouseManagerAdapter extends RecyclerView.Adapter<BaseHolder> {

    private Context context;
    private List<HouseManagerBean> data;

    public HouseManagerAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0){
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.lv_empty_view,parent,false));
        }else {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_house_manager, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        holder.instanceView(position);
    }


    @Override
    public int getItemCount() {
        if (data == null){
            return 0;
        }else if (data.size() == 0){
            return 1;
        }else {
            return data.size();
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (data == null){
            return -1;
        }else if (data.size() == 0){
            return 0;
        }else {
            return 1;
        }
    }

    private class ViewHolder extends BaseHolder {

        private ImageView icon;
        private TextView name;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void assignViews() {
            icon = (ImageView) findViewById(R.id.iv_icon);
            name = (TextView) findViewById(R.id.tv_name);
        }

        @Override
        public void instanceView(int position) {
            HouseManagerBean bean = data.get(position);
            icon.setImageResource(bean.getIconRes());
            name.setText(bean.getName());
            final int type = bean.getType();
            getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (type == HouseListActivity.TYPE_RENT_ROOM) {
                        Intent intent = new Intent(context, CardAccreditActivity.class);
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, HouseListActivity.class);
                        intent.putExtra(HouseListActivity.TYPE, type);
                        context.startActivity(intent);
                    }
                }
            });
        }
    }

    public void notifyData(List<HouseManagerBean> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

}
